package at.fhooe.stefanirndorfer.entity;


import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.util.Random;

import org.lwjgl.BufferUtils;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;

import static org.lwjgl.opengl.GL11.*;
import at.fhooe.stefanirndorfer.engine.game.Game;
import at.fhooe.stefanirndorfer.sound.SoundManager;
import at.fhooe.stefanirndorfer.sound.SimpleSoundSoundPlayer;
import at.fhooe.stefanirndorfer.sound.SoundPlayerEFX;
import at.stefanirndorfer.openaltest.entity.Entity;
import at.stefanirndorfer.openaltest.input.InputManager;
import at.stefanirndorfer.openaltest.input.KeyInputListener;
import at.stefanirndorfer.openaltest.input.MouseInputListener;
import at.stefanirndorfer.openaltest.logging.Log;

@SuppressWarnings("unused")
public class CircleReflection extends Entity implements KeyInputListener, MouseInputListener{
	
	private static final int STEPWIDTH = 5;
	private static final int DEFAULT_DELAY = 100;
	
	float [] color;
	
	int windowWidth;
	int windowHeight;
	private boolean isInBounds;
	private boolean selected;
	
	//sound stuff
	private SoundManager sm;
	private String[] soundIds;
	
	
	InputManager im;

	private int currentDelay;
	
	//TODO on Destroy muss der soundPlayer terminiert werden (sourceBuffer und was ist mit buffer?)
	
	public CircleReflection(int width, int height, double initPositionX, double initPositionY, float[] color, String[] soundIds){
		super(width, height, initPositionX, initPositionY);
		im = Game.getInstance().getInputManager();
		posX = initPositionX;
		posY = initPositionY;
		currentDelay = DEFAULT_DELAY;
		Log.info(this, "pos x and y: " + posX +" , " + posY);
		this.width = width;
		this.height = height;
		this.color = new float[3];
		randomizeColor();

		//in order to process input
		setUpKeyInputListener();
		setUpMouseInputListener();
		windowWidth = Game.getInstance().getWindow().getWindowWidth();
		windowHeight = Game.getInstance().getWindow().getWindwoHeight();
		
		isInBounds = false;
		selected = false;
		
		sm = Game.getInstance().getSoundManager();
		//initSoundIds();
		this.soundIds = soundIds;
	
		soundPlayerEFX = new SoundPlayerEFX(sm, soundIds, currentDelay, initPositionX, initPositionY, width, height);
	}

	private void randomizeColor() {
		Random rand = new Random();
		this.color[0] = rand.nextFloat();
		this.color[1] = rand.nextFloat();
		this.color[2] = rand.nextFloat();
	}



	@Override
	public void setUpMouseInputListener() {
		Game.getInstance().getInputManager().addMouseListener(this);
	}

	@Override
	public void tearDownMouseInputListener(MouseInputListener il) {
		Game.getInstance().getInputManager().removeMouseListener(this);		
	}


	@Override
	public void setUpKeyInputListener() {
		Game.getInstance().getInputManager().addKeyListener(this);				
	}

	@Override
	public void tearDownKeyInputListener(KeyInputListener il) {
		Game.getInstance().getInputManager().removeKeyListener(this);		
		
	}

	@Override
	public void processKeyInput(int eventKey, boolean keyDown) {
		if(keyDown){
			if(eventKey == Keyboard.KEY_UP && keyDown){
				posY += STEPWIDTH;
			}
			if(eventKey == Keyboard.KEY_DOWN && keyDown){
				posY -= STEPWIDTH;
			}
			if(eventKey == Keyboard.KEY_LEFT && keyDown){
				posX -= STEPWIDTH;
			}
			if(eventKey == Keyboard.KEY_RIGHT && keyDown){
				posX += STEPWIDTH;
			}
			if(eventKey == Keyboard.KEY_1 && keyDown){
				//soundPlayerEFX.play("tractor_beam");
			}
			//fadeout
			if(eventKey == Keyboard.KEY_2 && keyDown){
				//soundPlayerEFX.play("Repair");
			}
			
		}else{
			
		}
	}

	@Override
	public void processMouseInput(int x, int y, int dx, int dy) {
		
		isOnThis(x, y);
		if(isInBounds){
			if(Mouse.isButtonDown(0) && !selected){
				selected = true;
				System.out.println("Mouse pushed");
		}else{
			if(!Mouse.isButtonDown(0)){
				selected = false;				
			}
		}
			if(Mouse.isButtonDown(2)){
				
			}
		} else{
			selected = false;
		}
	}


	private void isOnThis(int mX, int mY) {
		mY = -mY;
		
		double xDist = mX - posX;
		double yDist = mY - posY;

		double distToCenter = Math.sqrt(xDist*xDist + yDist*yDist);
		if(distToCenter < width){
			isInBounds = true;
		}else{
			isInBounds = false;
		}
	}

	@Override
	public void render() {		
		glColor3f(color[0], color[1], color[2]);

		glBegin(GL_TRIANGLE_FAN);
		for (int angle = 0; angle < 360; angle+=5) {
			glVertex2d(posX + Math.sin(angle) * width, posY + Math.cos(angle) * width);
		}
		glEnd();
	}

	@Override
	public void update(int delta) {
		if(selected){
			
			float oldX = (float) posX;
			float oldY = (float) posY;
			
			
			posX += im.dx;
			//mouse dy needs to be inverted
			posY += im.dy;
			
			float posXf = (float)(posX)- oldX;		
			float posYf = (float)(posY) - oldY;
			float[] sourceParams = new float[]{posXf, posYf};
			
			soundPlayerEFX.updateSourceParams(sourceParams);
		}
			
	}

	@Override
	public double[] getPosition() {
		return new double[]{posX,posY};
	}

	/**
	 * sounds without ".wav" 
	 */
	private void initSoundIds() {
		this.soundIds = new String[]{
				"Repair",
				"tractor_beam",
		};
	}
	
	
	SoundPlayerEFX soundPlayerEFX;
	public SoundPlayerEFX getSoundPlayerEFX() {
		return soundPlayerEFX;
	}


}
